/**
 * created by 小卷毛, 2019/9/6
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.af.community.smart.view

import android.content.Context
import android.text.method.DigitsKeyListener
import android.view.Gravity
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.af.community.smart.business.R
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.listeners.onClick
import android.text.InputFilter
import org.jetbrains.anko.sdk25.listeners.textChangedListener

/**
 * 项目名称：Smart_Business
 * 创建人：41614
 * 创建时间：2019-09-06 22:12
 */

fun Context.createSelectView(
    isRequired: Boolean = false,
    title: String = "",
    content: String = "",
    hintText: String = "",
    action: String = "",
    position: Int = -1,
    listener: (TextView, String, Int) -> Unit = { _, _, _ -> }
) = UI {
    verticalLayout {
        lparams(width = matchParent)
        linearLayout {
            imageView {
                imageResource = R.mipmap.icon_dot
                visibility = if (isRequired) View.VISIBLE else View.INVISIBLE
            }.lparams(width = dip(8)) {
                marginStart = dip(6)
            }

            themedTextView(
                title,
                R.style.Font16_black
            ).lparams {
                marginStart = dip(6)
                marginEnd = dip(15)
            }

            val contentView = themedTextView(
                content,
                R.style.Font16_black
            ) {
                id = R.id.select_tag
                gravity = Gravity.END
                hint = hintText
            }.lparams(weight = 1f) {
                marginEnd = dip(if (action.isNotEmpty()) 10 else 15)
            }

            if (action.isNotEmpty()) {
                imageView(R.mipmap.icon_arr01).lparams(
                    width = dip(7),
                    height = dip(12)
                ) {
                    marginEnd = dip(15)
                }
            }

            gravity = Gravity.CENTER_VERTICAL
            onClick {
                listener.invoke(contentView, action, position)
            }
        }.lparams(
            width = matchParent,
            height = dip(55)
        )

        view {
            backgroundColorResource = R.color.divider_dark
        }.lparams(
            width = matchParent,
            height = dip(0.5f)
        ) {
            marginStart = dip(15)
            marginEnd = dip(15)
        }
    }
}.view

@Suppress("DEPRECATION")
fun Context.createStableInputView(
    isRequired: Boolean = false,
    title: String = "",
    content: String = "",
    hintText: String = "",
    digits: String = "",
    type: String = "",
    length: Int = -1,
    position: Int = -1,
    listener: (String, Int) -> Unit = { _, _ -> }
) = UI {
    verticalLayout {
        lparams(width = matchParent)
        linearLayout {
            imageView {
                imageResource = R.mipmap.icon_dot
                visibility = if (isRequired) View.VISIBLE else View.INVISIBLE
            }.lparams(width = dip(8)) {
                marginStart = dip(6)
            }

            themedTextView(
                title,
                R.style.Font16_black
            ).lparams {
                marginStart = dip(6)
            }

            themedEditText(content, R.style.Font16_black) {
                backgroundColorResource = R.color.white
                gravity = Gravity.END or Gravity.CENTER_VERTICAL
                horizontalPadding = dip(15)
                hint = hintText

                if (length > 0) { //限制输入位数
                    filters = arrayOf<InputFilter>(InputFilter.LengthFilter(length))
                }

                maxLines = 1 //需inputType限制，否则无效
                when (type) {
                    "text" -> { //限制输入英文字母和数字
                        inputType = EditorInfo.TYPE_TEXT_VARIATION_URI
                        if (digits.isNotEmpty()) {
                            this.keyListener = object : DigitsKeyListener() {
                                override fun getInputType() = EditorInfo.TYPE_TEXT_VARIATION_URI
                                override fun getAcceptedChars() = digits.toCharArray()
                            }
                        }
                    }
                    "number" -> { //限制输入数字
                        inputType = EditorInfo.TYPE_CLASS_NUMBER
                        if (digits.isNotEmpty()) {
                            this.keyListener = object : DigitsKeyListener() {
                                override fun getInputType() = EditorInfo.TYPE_CLASS_NUMBER
                                override fun getAcceptedChars() = digits.toCharArray()
                            }
                        }
                    }
                    else -> { //默认输入
                        inputType = EditorInfo.TYPE_CLASS_TEXT
                        if (digits.isNotEmpty()) {
                            this.keyListener = object : DigitsKeyListener() {
                                override fun getInputType() = EditorInfo.TYPE_CLASS_TEXT
                                override fun getAcceptedChars() = digits.toCharArray()
                            }
                        }
                    }
                }

                textChangedListener {
                    onTextChanged { charSequence, _, _, _ ->
                        listener.invoke(charSequence?.toString().orEmpty(), position)
                    }
                }
            }.lparams(
                width = matchParent,
                height = matchParent
            )

            gravity = Gravity.CENTER_VERTICAL
        }.lparams(
            width = matchParent,
            height = dip(55)
        )

        view {
            backgroundColorResource = R.color.divider_dark
        }.lparams(
            width = matchParent,
            height = dip(0.5f)
        ) {
            marginStart = dip(15)
            marginEnd = dip(15)
        }
    }
}.view

fun Context.createVariableInputView(
    isRequired: Boolean = false,
    title: String = "",
    content: String = "",
    hintText: String = "",
    length: Int = -1,
    position: Int = -1,
    listener: (String, Int) -> Unit = { _, _ -> }
) = UI {
    verticalLayout {
        lparams(width = matchParent)
        linearLayout {
            imageView {
                imageResource = R.mipmap.icon_dot
                visibility = if (isRequired) View.VISIBLE else View.INVISIBLE
            }.lparams(
                width = dip(8),
                height = dip(55)
            ) {
                marginStart = dip(6)
            }

            themedTextView(
                title,
                R.style.Font16_black
            ) {
                gravity = Gravity.CENTER_VERTICAL
            }.lparams(height = dip(55)) {
                marginStart = dip(6)
            }

            themedEditText(content, R.style.Font16_black) {
                backgroundColorResource = R.color.white
                gravity = Gravity.END or Gravity.CENTER_VERTICAL
                padding = dip(15)
                minHeight = dip(55)
                setLineSpacing(dip(5).toFloat(), 1.0f)
                hint = hintText

                if (length > 0) { //限制输入位数
                    filters = arrayOf<InputFilter>(InputFilter.LengthFilter(length))
                }

                textChangedListener {
                    onTextChanged { charSequence, _, _, _ ->
                        listener.invoke(charSequence?.toString().orEmpty(), position)
                    }
                }
            }.lparams(
                width = matchParent,
                height = wrapContent
            )
        }.lparams(
            width = matchParent,
            height = wrapContent
        )

        view {
            backgroundColorResource = R.color.divider_dark
        }.lparams(
            width = matchParent,
            height = dip(0.5f)
        ) {
            marginStart = dip(15)
            marginEnd = dip(15)
        }
    }
}.view